package com.meida.student.app.user.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.client.entity.BusSubject;
import com.meida.module.bus.client.entity.BusTutorType;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.List;

/**
 * 辅导类型接口
 * @author zyf
 */
@Component
public class TutorTypeListHandler implements PageInterceptor<BusTutorType> {

    @Override
    public void prepare(CriteriaQuery<BusTutorType> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        cq.select(BusTutorType.class, "tutorTypeId", "tutorTypeName","chargeType", "tutorPrice", "priceUnit");
        cq.eq(BusTutorType.class, "tutorState", 1);
        cq.eq(BusSubject.class, "subjectId");
        cq.createJoin(BusSubject.class).setMainField("subjectId").setJoinField("subjectId");
    }

    @Override
    public void complete(CriteriaQuery<BusTutorType> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String tutorTypeName=entityMap.get("tutorTypeName");
            BigDecimal tutorPrice=entityMap.getBigDecimal("tutorPrice");
            String priceUnit=entityMap.get("priceUnit");
            String tutorTitle= MessageFormat.format("{0}(${1}/{2})",tutorTypeName,tutorPrice,priceUnit);
            entityMap.put("tutorTitle",tutorTitle);
            entityMap.remove("tutorTypeName");
            entityMap.remove("priceUnit");
        }
    }
}
